<?php
namespace plugins\member\controller;
use cmf\controller\PluginRestBaseController;//引入此类
use think\Db;
use plugins\member\model\MemberModel;
class ApiPublicController extends PluginRestBaseController
{
	//忘记密码
	public function forgetPassword()
	{
		//传入手机号与验证码 传入新密码
		if($this->request->isPost()){
			$post = $this->request->post();
			if($post['password'] == ''){
				return api_json_echo(false,'密码不能为空');
			}
			if($post['re_password'] == ''){
				return api_json_echo(false,'重复密码不能为空');
			}
			if($post['password'] != $post['re_password']){
				return api_json_echo(false,'两次输入的密码不相同');
			}
			$member = ( new MemberModel )->get(['phone'=>$post['phone']]);
			if($member == null){
				return api_json_echo(false,'错误的手机号码');
			}
			$member->password = '';
			$member->save();
			$member->password = cmf_password($post['password']);
			$result = $member->save();
			if($result){
				return api_json_echo(true,'修改完成');
			}
			return api_json_echo(false,'修改失败');
		}
		return api_json_echo(false,'查询失败,错误的地址');
	}

	//登录前发短信
	public function sendCode()
	{
		$post = $this->request->post();
		if(!preg_match("/^1[34578]\d{9}$/", $post['phone'])){
			return api_json_echo( false , '请输入正确的手机号' );
		}
		//验证码生成
		$code = cmf_get_verification_code($post['phone'], 6);
		if($code == false){
			return api_json_echo( false , '发送失败，今日发送次数已达到15次上限' , $code );
		}
		//验证码发送
		$data = [
            'mb_name' => 'verify_code', //模板标识,项目中可自定义,定义以后到短信配置做相应的模板配置, 但是在写标准基础模块和插件时必须按照规范书写,方便复用
            'mobile' => $post['phone'],//手机号
            'content' => [  //发送内容,用于存放模板变量, 默认可以设置 [] 空数组, 
                'code' => $code
            ]
        ]; 
        $return = cmf_sms_send( $data );
        if($return['status'] == true){
	 		//更新验证码发送日志
			$result = cmf_verification_code_log($post['phone'], $code, (time()+300));
			if($result){
				return api_json_echo( true , '发送成功');
			}else{
				return api_json_echo( false , '发送失败');
			}     	
        }else{
        	return api_json_echo( false , '发送失败');
        }
	}	

	//发短信前检查该手机号是否已注册
	public function sendCodeCheck()
	{
		$post = $this->request->post();
		if(!preg_match("/^1[34578]\d{9}$/", $post['phone'])){
			return api_json_echo( false , '请输入正确的手机号' );
		}

		//验证手机号是否已注册
		$check = ( new MemberModel )->get(['phone'=>$post['phone']]);

		if(!$check){
			return api_json_echo(false,'该手机号未注册！请检查');
		}

		//验证码生成
		$code = cmf_get_verification_code($post['phone'], 6);
		if($code == false){
			return api_json_echo( false , '发送失败，今日发送次数已达到15次上限' , $code );
		}
		//验证码发送
		$data = [
            'mb_name' => 'verify_code', //模板标识,项目中可自定义,定义以后到短信配置做相应的模板配置, 但是在写标准基础模块和插件时必须按照规范书写,方便复用
            'mobile' => $post['phone'],//手机号
            'content' => [  //发送内容,用于存放模板变量, 默认可以设置 [] 空数组, 
                'code' => $code
            ]
        ]; 
        $return = cmf_sms_send( $data );
        if($return['status'] == true){
	 		//更新验证码发送日志
			$result = cmf_verification_code_log($post['phone'], $code, (time()+300));
			if($result){
				return api_json_echo( true , '发送成功');
			}else{
				return api_json_echo( false , '发送失败');
			}     	
        }else{
        	return api_json_echo( false , '发送失败');
        }		
	}


	//登陆前检查短信验证码
	public function checkCode()
	{
		//phone code
		$post = $this->request->post();
		if(!preg_match("/^1[34578]\d{9}$/", $post['phone'])){
			return api_json_echo( false , '请检查手机号' );
		}		
		//手机验证码检测
		$check_code = cmf_check_verification_code($post['phone'], $post['code'],true);
		if($check_code != ''){
			return api_json_echo( false , $check_code);
		}
		return api_json_echo( true , '验证成功');
	}


}